<template>
  <el-dialog append-to-body :close-on-click-modal="false" :before-close="crud.cancelCU" :visible="crud.status.cu > 0" :title="crud.status.title" width="700px">
    <el-form ref="form" :model="form" :rules="rules" size="small" label-width="120px">
      <el-form-item label="上级栏目" prop="sectionParentId">
        <treeselect v-model="form.sectionParentId" :options="channel" :normalizer="treeNormalizer" style="width: 450px;" placeholder="选择上级栏目" />
      </el-form-item>
      <el-form-item label="栏目名称" prop="sectionName">
        <el-input v-model="form.sectionName" />
      </el-form-item>
      <el-form-item label="栏目图标" prop="sectionBanner">
        <el-input v-model="form.sectionBanner" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="text" @click="crud.cancelCU">取消</el-button>
      <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
    </div>
  </el-dialog>
</template>

<script>
import CRUD, { form } from '@crud/crud'
import { getSectionTableTree } from '@/api/business/common/article/article-channel'
import Treeselect from '@riophae/vue-treeselect'

const defaultForm = { sectionId: null, sectionName: null, sectionParentId: null, sectionBanner: null, level: null, level01Id: null, level02Id: null, level03Id: null, level04Id: null, level05Id: null, isNode: null }
export default {
  components: { Treeselect },
  mixins: [form(defaultForm)],
  data() {
    return {
      channel: [],
      rules: {
        sectionName: [{ required: true, message: '栏目名称不能为空', trigger: 'blur' }],
        sectionParentId: [{ required: true, message: '上级栏目不能为空', trigger: 'blur' }]
      }
    }
  },
  methods: {
    [CRUD.HOOK.afterToCU](crud, form) {
      getSectionTableTree().then(res => {
        this.channel = []
        const menu = { sectionId: 0, sectionName: '顶级类目', childs: [] }
        menu.childs = res
        this.channel.push(menu)
      })
    },
    treeNormalizer(node) {
      return {
        id: node.sectionId,
        label: node.sectionName,
        children: node.childs
      }
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  /deep/ .el-input-number .el-input__inner {
    text-align: left;
  }
</style>
